﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Components.Users;
using Framework.Library.Utilities;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Users
{
	public partial class UserPassword : AuthenticatedControl
	{
		private int userId;
		protected void Page_Load(object sender, EventArgs e)
		{
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            lblUpdateStatus.Text = string.Empty;
            userId = ConvertUtility.ToInt32(Request.QueryString["uid"]);
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())) || (userId == 0))
                Response.Redirect(AppEnv.ADMIN_CMD, true);			        
			if (!Page.IsPostBack)
			{				
				lblUsername.Text = CurrentUser.Username;
                lblCommandName.Text = AppEnv.CommandName();
			}
		}
		protected void btnUpdate_Click(object sender, EventArgs e)
		{
            if ((UserController.ValidateUser(lblUsername.Text.Trim(), SecurityMethod.MD5Encrypt(txtCurrentPassword.Text.Trim())) != null))
            {                
                try
                {
                    UserController.ResetPassword(userId, SecurityMethod.MD5Encrypt(txtPassword.Text.Trim()));
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                }
                catch (Exception ex)
                {
                    lblUpdateStatus.Text = ex.Message;
                }
            }
            else
                lblUpdateStatus.Text = "Mật khẩu không đúng";
		}
		protected void btnCancel_Click(object sender, EventArgs e)
		{
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.UserManage)));
		}
	}
}